package com.xinlong.dao;

import com.xinlong.entity.Role;
import com.xinlong.entity.Userinfo;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

public interface IUserDao {

    @Select("select * from users where username=#{name}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "email",property = "email"),
            @Result(column = "password",property = "password"),
            @Result(column = "phoneNum",property = "phoneNum"),
            @Result(column = "status",property = "status"),
            @Result(column = "id",property = "roles",javaType = List.class,many = @Many(select = "com.xinlong.dao.IRolesdao.findbyusername")),
    })
    Userinfo findbyname(String name);

    @Select("select * from users")
    List<Userinfo> Selectuser();

    @Insert("insert into users(email,username,password,phoneNum,status) value(#{email},#{username},#{password},#{phoneNum},#{status})")
    void save(Userinfo uu);


    @Select("select * from users where id=#{id}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "email",property = "email"),
            @Result(column = "password",property = "password"),
            @Result(column = "phoneNum",property = "phoneNum"),
            @Result(column = "status",property = "status"),
            @Result(column = "id",property = "roles",javaType = List.class,many = @Many(select = "com.xinlong.dao.IRolesdao.findbyusername")),
    })
    Userinfo findbyid(String id);


    @Select("select * from role where id not in( select roleId from users_role where userId=#{id})")
    List<Role> notUserbyRoleid(String id);


    @Insert("insert into users_role(userId,roleId) values(#{userid},#{roleid})")
    void addRoleToUser(@Param("userid") String userid, @Param("roleid")String roleid);
}
